﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Service;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.Data;
using PersonalHealth.Data.IService;
using Core.UnitOfWork;
using PersonalHealth.Data.Repository;

namespace PersonalHealth.Data.Service
{
    public class MessageService : Service<Message, HealthContext>, IMessageService
    {
        public int GetNotReadMessageCount(int userid)
        {
            using (HealthContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IMessageRepository repository = new MessageRepository(unitOfWork);
                return repository.GetNotReadMessageCount(userid);
            }
        }


        public IList<Message> GetUserMessages(int userid, int pageIndex, int pageCount, out int msgCount, out int notReadCount)
        {
            using (HealthContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IMessageRepository repository = new MessageRepository(unitOfWork);
                return repository.GetUserMessages(userid, pageIndex, pageCount, out msgCount, out notReadCount);
            }
        }

        public IList<Message> GetUserMessages(int userid, MessageType type, int pageIndex, int pageCount, out int msgCount, out int notReadCount)
        {
            using (HealthContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IMessageRepository repository = new MessageRepository(unitOfWork);

                return repository.GetUserMessages(userid, type, pageIndex, pageCount, out msgCount, out notReadCount);
            }

        }

        public void SetStateToHasRead(int id)
        {
            using (HealthContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IMessageRepository repository = new MessageRepository(unitOfWork);

                var obj = repository.Get(id);
                obj.NotRead = false;
                repository.Update(obj);
                unitOfWork.Save();
            }
        }
    }
}
